
local items = {
    settingsfile = "戒律牧PVE.json",
    {
        type = "entry",
        text = "|cff00ccff修改 |cffffffff- 戒律牧PVE - |cff888888v1.1.0",
        tooltip ="" ,
    },
    {
        type = "separator",
    },
    {
        type = "title",
        text = "|cff00ccff基础设定",
    },
    {
        type = "separator",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48168)) .. ":26:26\124t 心灵之火",
        tooltip = "Cast Inner Fire if not active",
        enabled = true,
        key = "innerfire",
    },
     {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48073)) .. ":26:26\124t 神圣之灵",
        tooltip = "Cast Inner Fire if not active",
        enabled = true,
        key = "DivineSpirit",
    },
    {
        type = "entry",
        text = "\124T" .. GetItemIcon(40211) .. ":26:26\124t 急速药水",
        tooltip = "使用急速药水",
        enabled = true,
        key = "speedpotion",
    },
   {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(28730)) .. ":26:26\124t 蓝低于%使用奥术洪流",
        tooltip = "aoshuhongliu",
        enabled = true,
        value = 20,
        key = "arcanetorrent",
    },
   {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(64901)) .. ":26:26\124t 蓝低于%使用希望圣歌",
        tooltip = "xiwangshengge",
        enabled = true,
        value = 20,
        key = "hymnofhope",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(34433)) .. ":26:26\124t 蓝低于%使用暗影魔",
        tooltip = "Cast Shadowfiend on target if you at or below mana percentage",
        enabled = true,
        value = 40,
        key = "shadowfiend",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(528)) .. ":26:26\124t 驱散疾病",
        tooltip = "Cast Cure Disease if you or ally has a disease debuff",
        enabled = true,
        key = "curedisease",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(988)) .. ":26:26\124t 驱散魔法",
        tooltip = "Cast Dispell Magic if you or ally has a magic debuff",
        enabled = true,
        key = "dispellmagic",
    },
    {
        type = "separator",
    },
    {
        type = "title",
        text = "|cff00ccff团队加血",
    },
    {
        type = "separator",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48072)) .. ":26:26\124t 团队4人血量低于%使用治疗祷言",
        tooltip = "Cast Prayer of Healing if more than 4 allys are at or below health percentage",
        enabled = true,
        value = 80,
        key = "prayerofhealing",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(53007)) .. ":26:26\124t 血量低于%使用苦修",
        tooltip = "Cast Penance if you or ally are at or below health percentage",
        enabled = true,
        value = 80,
        key = "penance",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48173)) .. ":26:26\124t 自身血量低于%使用绝望祷言",
        tooltip = "Cast desperateprayer if you or ally are at or below health percentage",
        enabled = true,
        value = 50,
        key = "desperateprayer",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48071)) .. ":26:26\124t 血量低于%使用快速治疗",
        tooltip = "Cast Flash Heal if you or ally are at or below health percentage. If you raid 25man i recommend too disable Flash Heal for Raid, because you dont use Flash Heal in 25man you rather prioritise too shield the entire raid",
        enabled = true,
        value = 60,
        key = "flashheal",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48066)) .. ":26:26\124t 血量低于%使用真言术：盾",
        tooltip = "Cast Power Word: Shield if you or ally are at or below health percentage",
        enabled = true,
        value = 80,
        key = "powerwordshieldlowhp",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48066)) .. ":26:26\124t 全团无脑真言术：盾",
        tooltip = "Cast Power Word: Shield on all raid member",
        enabled = true,
        key = "powerwordshieldraid",
    },
    {
        type = "separator",
    },
    {
        type = "title",
        text = "|cff00ccff坦克治疗",
    },
    {
        type = "separator",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48068)) .. ":26:26\124t 恢复",
        tooltip = "Cast Renew on tank",
        enabled = true,
        key = "renewtank",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48113)) .. ":26:26\124t 愈合祷言",
        tooltip = "Cast Prayer of Mending on tank",
        enabled = true,
        key = "prayerofmendingtank",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48066)) .. ":26:26\124t 真言术：盾",
        tooltip = "Cast Power Word: Shield on tank",
        enabled = true,
        key = "powerwordshieldtank",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(33206)) .. ":26:26\124t 坦克血量低于%使用压制",
        tooltip = "Cast Pain Suppression if tank is at or below health percentage",
        enabled = true,
        value = 40,
        key = "painsuppressiontank",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(48071)) .. ":26:26\124t 坦克血量低于%使用快速治疗",
        tooltip = "Cast Flash Heal if tank is at or below health percentage",
        enabled = true,
        value = 80,
        key = "flashhealtank",
    },
    {
        type = "entry",
        text = "\124T" .. select(3, GetSpellInfo(53007)) .. ":26:26\124t 坦克血量低于%使用苦修",
        tooltip = "Cast Penance if tank is at or below health percentage",
        enabled = true,
        value = 80,
        key = "penancetank",
    },
}


local function GetSetting(name)
    for k, v in ipairs(items) do
        if v.type == "entry"
        and v.key ~= nil
        and v.key == name then
            return v.value, v.enabled
        end
    end
end

local function onload()
    ni.GUI.AddFrame("修改戒律牧PVE", items);
end

local function onunload()
    ni.GUI.DestroyFrame("修改戒律牧PVE");
end

local spell = {
    innerfire = {id = 48168, name = GetSpellInfo(48168)},  --心灵之火
    DivineSpirit = {id = 48073, name = GetSpellInfo(48073)},  --神圣之灵
    desperateprayer = {id = 48173, name = GetSpellInfo(48173)},--绝望祷言
    painsuppression = {id = 33206, name = GetSpellInfo(33206)},
    powerinfusion = {id = 10060, name = GetSpellInfo(10060)},
    shadowfiend = {id = 34433, name = GetSpellInfo(34433)},  --暗影恶魔
    curedisease = {id = 528, name = GetSpellInfo(528)},  --祛病术
    dispelmagic = {id = 988, name = GetSpellInfo(988)},  --驱散魔法
    renew = {id = 48068, name = GetSpellInfo(48068)},  --恢复
    prayerofmending = {id = 48113, name = GetSpellInfo(48113)},  --愈合祷言
    prayerofhealing = {id = 48072, name = GetSpellInfo(48072)},  --治疗祷言
    penance = {id = 53007, name = GetSpellInfo(53007)},  --苦修
    flashheal = {id = 48071, name = GetSpellInfo(48071)},  --快速治疗
    powerwordshield = {id = 48066, name = GetSpellInfo(48066)},  --真言术：盾
    weakenedsoul = {id = 6788, name = GetSpellInfo(6788)},  --虚弱灵魂
    arcanetorrent = {id = 28730, name = GetSpellInfo(28730)},  --奥术洪流
    hymnofhope = {id = 64901, name = GetSpellInfo(64901)},  --希望圣歌
}

local item = {
    food = GetSpellInfo(45548),
    drink = GetSpellInfo(57073),
    speedpotion = GetItemInfo(70133),
}

local queue = {
    "Inner Fire",
    "DivineSpirit",
--    "Pause Rotation",
    "Shadowfiend",
   "Arcane Torrent",
    "Hymn of Hope",
"Pain Suppression (Tank)",
    "Desperate Prayer",
    "Glowing Twilight Scale",
    "Power Word: Shield (Tank)",
    "Power Word: Shield (Low HP)",
    "Prayer of Mending (Tank)",
    "Prayer of Healing",
    "Penance (Tank)",
    "Penance",
    "Flash Heal (Tank)",
    "Flash Heal",
    "Renew (Tank)",
    "Cure Disease",
    "Dispel Magic",
    "Power Word: Shield (Raid)",
    "Speed Potion",
    "Auto Target",
}

local abilities = {
   
    ["Inner Fire"] = function()
        local _, enabled = GetSetting("innerfire")
        if enabled then
            if not ni.player.buff(spell.innerfire.id) then
                ni.spell.cast(spell.innerfire.name)
                return true;
            end
        end
    end,

    ["DivineSpirit"] = function()
        local _, enabled = GetSetting("DivineSpirit")
        if enabled then
            if not ni.player.buffs("48073||48074") then
                ni.spell.cast(spell.DivineSpirit.name)
                return true;
            end
        end
    end,

    ["Pause Rotation"] = function()
        if IsMounted()
        or UnitIsDeadOrGhost("player")
        or UnitUsingVehicle("player")
        or UnitInVehicle("player")
        or ni.unit.ischanneling("player")
        or ni.unit.iscasting("player")
        or ni.unit.buff("player", item.food)
        or ni.unit.buff("player", item.drink) then
            return true;
        end
    end,

    ["Desperate Prayer"] = function()
        local value, enabled = GetSetting("desperateprayer")
        if enabled then
            if ni.spell.available(spell.desperateprayer.id)
            and ni.player.hp() < value then
               ni.spell.cast(spell.desperateprayer.name)
                return true;
            end
        end
    end,

    ["Arcane Torrent"] = function()
        local value, enabled = GetSetting("arcanetorrent")
        if enabled then
            if ni.spell.available(spell.arcanetorrent.id)
            and ni.player.power() < value then
                ni.spell.cast(spell.arcanetorrent.name)
                return true;
            end
        end
    end,


    ["Shadowfiend"] = function()
        local value, enabled = GetSetting("shadowfiend")
        if enabled then
            if ni.spell.available(spell.shadowfiend.id)
            and ni.spell.valid("target", spell.shadowfiend.id, true, true)
            and ni.unit.exists("target")
            and ni.player.power() < value then
                ni.spell.cast(spell.shadowfiend.name, "target")
                return true;
            end
        end
    end,

    ["Hymn of Hope"] = function()
        local value, enabled = GetSetting("hymnofhope")
        if enabled then
            if ni.spell.available(spell.hymnofhope.id)
            and not ni.player.ismoving()
            and ni.player.power() < value then
               ni.spell.cast(spell.hymnofhope.name)
                return true;
            end
        end
    end,

    ["Cure Disease"] = function()
        local _, enabled = GetSetting("curedisease")
        if enabled then
            for i = 1, #ni.members do
                if ni.members[i]:debufftype("Disease")
                and ni.spell.available(spell.curedisease.id)
                and ni.spell.valid(ni.members[i].unit, spell.curedisease.id, false, true, true) then
                    ni.spell.cast(spell.curedisease.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Dispel Magic"] = function()
        local _, enabled = GetSetting("dispellmagic")
        if enabled then
            for i = 1, #ni.members do
                if ni.members[i]:debufftype("Magic")
                and ni.spell.available(spell.dispelmagic.id)
                and ni.spell.valid(ni.members[i].unit, spell.dispelmagic.id, false, true, true) then
                    ni.spell.cast(spell.dispelmagic.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Power Word: Shield (Tank)"] = function()
        local _, enabled = GetSetting("powerwordshieldtank")
        if enabled then
            for i = 1, #ni.members do
                if (ni.members[i].name == ni.vars.units.mainTank or ni.members[i].name == ni.vars.units.offTank)
                and not (ni.unit.debuff(ni.members[i].unit, spell.weakenedsoul.id) or ni.unit.buff(ni.members[i].unit, spell.powerwordshield.id))
                and ni.spell.valid(ni.members[i].unit, spell.powerwordshield.id, false, true, true) then
                    ni.spell.cast(spell.powerwordshield.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Penance (Tank)"] = function()
        local value, enabled = GetSetting("penancetank")
        if enabled then
            for i = 1, #ni.members do
                if (ni.members[i].name == ni.vars.units.mainTank or ni.members[i].name == ni.vars.units.offTank)
                and ni.unit.hp(ni.members[i].name) < value
                and ni.spell.available(spell.penance.id)
                and ni.spell.valid(ni.members[i].unit, spell.penance.id, false, true, true)
                and not ni.player.ismoving() then
                    ni.spell.cast(spell.penance.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Pain Suppression (Tank)"] = function()
        local value, enabled = GetSetting("painsuppressiontank")
        if enabled then
            for i = 1, #ni.members do
                if (ni.members[i].name == ni.vars.units.mainTank or ni.members[i].name == ni.vars.units.offTank)
                and ni.unit.hp(ni.members[i].name) < value
                and ni.spell.available(spell.painsuppression.id)
                and ni.spell.valid(ni.members[i].unit, spell.painsuppression.id, false, true, true) then
                    ni.spell.cast(spell.painsuppression.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Renew (Tank)"] = function()
        local _, enabled = GetSetting("renewtank")
        if enabled then
            for i = 1, #ni.members do
                if (ni.members[i].name == ni.vars.units.mainTank or ni.members[i].name == ni.vars.units.offTank)
                and not ni.members[i]:buff(spell.renew.id, "player")
                and ni.spell.valid(ni.members[i].unit, spell.renew.id, false, true, true) then
                    ni.spell.cast(spell.renew.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Prayer of Mending (Tank)"] = function()
        local _, enabled = GetSetting("prayerofmendingtank")
        if enabled then
            for i = 1, #ni.members do
                if (ni.members[i].name == ni.vars.units.mainTank or ni.members[i].name == ni.vars.units.offTank)
                and not ni.unit.buff(ni.members[i].unit, spell.prayerofmending.id, "player")
                and ni.spell.available(spell.prayerofmending.id)
                and ni.spell.valid(ni.members[i].unit, spell.prayerofmending.id, false, true, true) then
                    ni.spell.cast(spell.prayerofmending.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Prayer of Healing"] = function()
        local value, enabled = GetSetting("prayerofhealing")
        if enabled then
            local count = ni.members.below(value);
            for i = 1, #ni.members do
                if count >= 4
                and ni.spell.valid(ni.members[i].unit, spell.prayerofhealing.id,false, true, true)
                and not ni.player.ismoving() then
                    ni.spell.cast(spell.prayerofhealing.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Penance"] = function()
        local value, enabled = GetSetting("penance")
        if enabled then
            for i = 1, #ni.members do
                if ni.unit.hp(ni.members[i].name) < value
                and ni.spell.available(spell.penance.id)
                and ni.spell.valid(ni.members[i].unit, spell.penance.id, false, true, true)
                and not ni.unit.ismoving("player") then
                    ni.spell.cast(spell.penance.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Flash Heal"] = function()
        local value, enabled = GetSetting("flashheal")
        if enabled then
            for i = 1, #ni.members do
                if ni.unit.hp(ni.members[i].name) < value
                and ni.player.buff(59891)
                and ni.unit.debuff(ni.members[i].unit,6788)
                and ni.spell.valid(ni.members[i].unit, spell.flashheal.id, false, true, true) 
                and not ni.unit.ismoving("player") then
                    ni.spell.cast(spell.flashheal.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Flash Heal (Tank)"] = function()
        local value, enabled = GetSetting("flashhealtank")
        if enabled then
            for i = 1, #ni.members do
                if (ni.members[i].name == ni.vars.units.mainTank or ni.members[i].name == ni.vars.units.offTank)
                and ni.unit.hp(ni.members[i].name) < value
                and ni.player.buff(59891)
                and ni.unit.debuff(ni.members[i].unit,6788)
                and ni.spell.valid(ni.members[i].unit, spell.flashheal.id, false, true, true)
                and not ni.unit.ismoving("player") then
                    ni.spell.cast(spell.flashheal.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Power Word: Shield (Low HP)"] = function()
        local value, enabled = GetSetting("powerwordshieldlowhp")
        if enabled then
            for i = 1, #ni.members do
                if ni.unit.hp(ni.members[i].name) < value
                and not (ni.unit.debuff(ni.members[i].unit, spell.weakenedsoul.id) or ni.unit.buff(ni.members[i].unit, spell.powerwordshield.id))
                and ni.spell.valid(ni.members[i].unit, spell.powerwordshield.id, false, true, true) then
                    ni.spell.cast(spell.powerwordshield.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Power Word: Shield (Raid)"] = function()
        local _, enabled = GetSetting("powerwordshieldraid")
        if enabled then
            for i = 1, #ni.members do
                if ni.unit.hp(ni.members[i].name) <= 100
                and not (ni.unit.debuff(ni.members[i].unit, spell.weakenedsoul.id) or ni.unit.buff(ni.members[i].unit, spell.powerwordshield.id))
                and ni.spell.valid(ni.members[i].unit, spell.powerwordshield.id, false, true, true) then
                    ni.spell.cast(spell.powerwordshield.name, ni.members[i].unit)
                    return true;
                end
            end
        end
    end,

    ["Speed Potion"] = function()
        local value, enabled = GetSetting("speedpotion")
        if enabled then
           if ni.unit.isboss("target")
           and ni.player.itemcd(item.speedpotion) == 0 then
              ni.player.useitem(item.speedpotion)
                return true;
            end
        end
    end,
}
ni.bootstrap.profile("戒律牧PVE", queue, abilities, onload, onunload)